package com.quanyan.club.task.tracker;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.club.entity.db.TbClub;
import com.quanyan.club.entity.db.TbClubExample;
import com.quanyan.club.mapper.ClubActivityMapper;
import com.quanyan.club.mapper.TbClubMapper;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.Limit;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by yue.yuan on 2017/3/13.
 */
@Component
public class ClubPlaceBindJob implements JobRunner {

    private static Logger logger = Logger.getLogger(ClubPlaceBindJob.class);

    @Autowired
    private ClubActivityMapper clubActivityMapper;
    @Autowired
    private TbClubMapper tbClubMapper;

    @Override
    public Result run(JobContext jobContext) throws Throwable {

        //查找有多少标记为未结束的
        int total = clubActivityMapper.countClubPlaceUnBind();
        Integer pageSize = 20;
        int successCount = 0;
        int all = 0;
        for (int i = 0; i < Math.ceil((double)total/pageSize); i++) {
            int pageNum = i + 1;
            Limit limit = Limit.buildLimit(pageNum, pageSize);
            TbClubExample example = new TbClubExample();
            example.createCriteria().andIsDeleteEqualTo(Constants.BYTE_ZERO)
                    .andPlaceLatGreaterThan(5.0).andResidentPlaceIdEqualTo(0);
            List<TbClub> clubs = tbClubMapper.selectByExample(example);
            for(TbClub tbClub : clubs) {
                //调用场馆接口
                Integer placeId = 0;
                if(null != placeId && placeId > 0) {
                    TbClub update = new TbClub();
                    update.setId(tbClub.getId());
                    update.setResidentPlaceId(placeId);
                    tbClubMapper.updateByPrimaryKeySelective(update);
                }
            }
        }
        return new Result(Action.EXECUTE_LATER);
    }
}
